#include "ShowFindUpdateRY.h"
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;

bool ShowFindUpdateRY::comp(Category pCat1, Category pCat2)
{
	return pCat1.getcatid() < pCat2.getcatid();
}

int ShowFindUpdateRY::countProdNumber(vector<Product> pProds, int pCatId)
{
	int tProdNumber=0,iProds;
	for (iProds=0;iProds<pProds.size();iProds++)
	{
		if (pProds.at(iProds).getcatid() == pCatId) {
			tProdNumber++;
		}
	}
	return tProdNumber;
}

void ShowFindUpdateRY::show_cats(vector<Category> pCats, vector<Product> pProds)
{
	int iCats,iProds,prodNumber;
	sort(pCats.begin(), pCats.end(), comp);
	cout << "showing all categories sorted by category id\n";
	cout << "id \tname \t\tnumber of products" << endl;
	for (iCats=0;iCats<pCats.size();iCats++)
	{	
		cout << pCats.at(iCats).display() << "\t\t" << countProdNumber(pProds,pCats.at(iCats).getcatid()) << endl;
	}
	/*cout << "Products\n";
	for (iCats=0;iCats<pProds.size();iCats++)
	{
		cout << pProds.at(iCats).display() << endl;
	}
	cout << "------------------------------------------------" << endl;*/
}

int ShowFindUpdateRY::countSaleNumber(vector<Cart_item> pCart_items, int pProd_id)
{
	//cart_items is empty o_O, just realized that it hasn't been filled yet..
	//cout << "countSaleNumber()" << endl;
	int tSaleNumber=0;
	int iCarts;
	//cout << "cart_items.size(): " << pCart_items.size() << endl;
	for (iCarts=0;iCarts<pCart_items.size();iCarts++)
	{
		cout << "tProdId: " << pCart_items.at(iCarts).getprodid() << endl;
		if (pCart_items.at(iCarts).getprodid() == pProd_id) {
			tSaleNumber++;
		}
	}
	return tSaleNumber;
}

void ShowFindUpdateRY::find(vector<Cart_item> pCart_items, vector<Product> pProds)
{
	int t_prod_id,iProds=0,tPrice;
	cout << "Input Product Id: ";
	cin >> t_prod_id;
	cout << "tProdId " << t_prod_id << endl;
	bool found = false;
	while (iProds<pProds.size() && !found)
	{
		cout << "prodId: " << pProds.at(iProds).getprodid() << endl;
		if (t_prod_id == pProds.at(iProds).getprodid()) {
			cout << "prod found" << endl; 
			tPrice = pProds.at(iProds).getprodprice();
			found = true;
		}
		iProds++;
	}
	if (!found) {
		cout << "product not found " << endl;
	} else {
		cout << "tPrice: " << tPrice << endl;
		int tSaleNumber = countSaleNumber(pCart_items, t_prod_id);
		cout << "tSaleNumber: " << tSaleNumber << endl;
		double totalSales = tSaleNumber * tPrice;
		cout << "total sales for the product: " << totalSales << endl;
	}
	
}

bool ShowFindUpdateRY::isExist(vector<Category> pCats, int pCatId)
{
	bool found=false;
	int iCats=0;
	while (iCats<pCats.size() && !found)
	{
		if (pCats.at(iCats).getcatid() == pCatId) {
			found=true;
		}
		iCats++;
	}
	return found;
}

void ShowFindUpdateRY::update(vector<Category> pCats, vector<Product> *pProds)
{
	int t_cat_id;
	show_cats(pCats, *pProds);
	cout << "Input Category Id: ";
	cin >> t_cat_id;
	cout << "t_cat_id: " << t_cat_id << endl;
	if (isExist(pCats,t_cat_id)) {
		int tprod_id;
		string tprod_name;
		double tprod_price;
		cout << "category exist" << endl;
		cout << "input product id: ";
		cin >> tprod_id;
		cout << "input product name: ";
		cin >> tprod_name;
		cout << "input product price: ";
		cin >> tprod_price;
		pProds->push_back(Product(tprod_id,t_cat_id,tprod_name,tprod_price));
	} else {
		cout << "category don't exist" << endl;
	}
	//pProds.push_back(pProd);
	
}